package com.haust.passport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haust.commons.exception.DaoException;
import com.haust.commons.pojo.HaustResult;
import com.haust.dao.RedisDao;
import com.haust.mapper.TbUserMapper;
import com.haust.passport.service.PassportService;
import com.haust.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;

@Service
public class PassportServiceImpl implements PassportService {
    @Autowired
    private TbUserMapper userMapper;

    @Autowired
    private RedisDao redisDao;

    @Value("${Haust.incr.user}")
    private String userKey;

    @Override
    public HaustResult checkUserInfo(String value, int flag) {
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        if (flag == 1) {
            //用户名
            queryWrapper.eq("username", value)
                    .or().eq("phone", value)
                    .or().eq("email", value);
        } else if (flag == 2) {
            //手机号
            queryWrapper.eq("username", value)
                    .or().eq("phone", value);
        } else if (flag == 3) {
            //邮箱
            queryWrapper.eq("username", value)
                    .or().eq("email", value);
        } else {
            return HaustResult.error("传递数据错误，flag=" + flag);
        }

        int count = userMapper.selectCount(queryWrapper);

        //查询不到，没有重复 ====》 可用
        if (count == 0) {
            return HaustResult.ok();
        }

        //有记录，有重复 =====》不可用
        return HaustResult.error("该账户已经注册");
    }

    @Override
    public HaustResult userRegister(TbUser tbUser) {
        //获取自增主键
        Long incr = null;
        try {
            incr = redisDao.incr(userKey);
        } catch (Exception e) {
            return HaustResult.error("服务忙，请稍后");
        }
        tbUser.setId(incr);
        Date date = new Date();
        tbUser.setCreated(date);
        tbUser.setUpdated(date);

        //密码加密
        tbUser.setPassword(DigestUtils.md5DigestAsHex(tbUser.getPassword().getBytes()));

        try {
            userMapper.insert(tbUser);
        }catch (DaoException e){
            throw new DaoException("服务忙，请稍后！");
        }

        return HaustResult.ok();
    }

    @Override
    public HaustResult userLogin(String username, String password) {
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .or().eq("phone", username)
                .or().eq("email", username);

        TbUser user = userMapper.selectOne(queryWrapper);

        if (user == null) {
            //用户名不存在
            return HaustResult.error("用户名不存在");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        if (!password.equals(user.getPassword())) {
            return HaustResult.error("密码输入错误");
        }
        //脱敏处理，将密码置空
        user.setPassword("");
        return HaustResult.ok(user);
    }
}
